package com.example.demo.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.demo.service.ExportService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 导出Controller
 *
 * @author xiaobeibei@gogpay.cn
 * @date 2023-12-08 14:33
 */
@RestController
@RequestMapping("/export")
public class ExportController {

    private final ExportService exportExcel;

    public ExportController(ExportService exportExcel) {
        this.exportExcel = exportExcel;
    }

    /**
     * 支持导出数据量不多
     *
     * @param form     前端入参
     * @param response http
     */
    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody @Validated JSONObject form, HttpServletResponse response) {
        exportExcel.exportExcel(form, response);
    }

    /**
     * 异步导出大量数据（使用mq消息队列实现）
     *
     * @param form 前端入参
     */
    @PostMapping("/exportBigData")
    public void exportBigData(@RequestBody @Validated JSONObject form) {
        exportExcel.exportBigData(form);
    }

    public static void main(String[] args) {
        // 提取表格所有日期正则表达式
        String pattern = "\\d{4}年\\d+月\\d+日";
        String pattern1 = "\\d{4}\\.\\d+\\.\\d+";
        String pattern2 = "\\d{4}/\\d+/\\d+";
        String pattern3 = "\\d{4}-\\d+-\\d+";
        Pattern r = Pattern.compile(pattern);
        Pattern r2 = Pattern.compile(pattern1);
        Pattern r3 = Pattern.compile(pattern2);
        Pattern r4 = Pattern.compile(pattern3);
//        Matcher matcher = r.matcher(s);
//        if (matcher.find()){
//            System.out.println(matcher.group());
//        }


//        String str = "2021-5-20";
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        LocalDate date = LocalDate.parse(str, formatter);
//        System.out.println(date);

        String dateString = "23";
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-M-dd");
        LocalDate localDate = LocalDate.parse(dateString, formatter1);
        System.out.println(localDate); // 输出: 2021-05-20

        // 报告中所有检测时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter formatter11 = DateTimeFormatter.ofPattern("yyyy-M-dd");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-M-d");
        LocalDate localDate1;
        try {
            localDate1 = LocalDate.parse(dateString, formatter);
        } catch (Exception s) {
            try {
                localDate1 = LocalDate.parse(dateString, formatter11);
            } catch (Exception s1) {
                localDate1 = LocalDate.parse(dateString, formatter2);
            }
        }
        if (localDate1.isAfter(LocalDate.now())){
            System.out.println("kk");
        }

    }

}
